package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.CompanyInfoMapper;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.CompanyInfoExample;
import com.suyuan.entity.TransportRouteInfor;
import com.suyuan.entity.TransportRouteInforExample;
import com.suyuan.service.CompanyInfoService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

@Service(value = "companyInfoService")
public class CompanyInfoServiceImpl implements CompanyInfoService{
    @Resource
    private CompanyInfoMapper companyInfoMapper;

    /**
     * 根据gid查询企业信息
     * @param gId
     * @return
     */
    public CompanyInfo findByGid(Integer gId){
        CompanyInfoExample companyInfoExample=new CompanyInfoExample();
        CompanyInfoExample.Criteria criteria=companyInfoExample.createCriteria();
        if(gId!=null){
            criteria.andGidEqualTo(gId);
            return companyInfoMapper.selectByPrimaryKey(gId);
        }
        return null;
    }

    /**
     *根据查询条件获取分页列表
     * @param gId 供应商编号
     * @param compName 供应商名称
     * @param compTypeId 企业类型编码
     * @param page 分页信息
     * @return
     */
    public PageInfo inquiry(Integer gId, String compName,Integer compTypeId, Page page) {
        CompanyInfoExample companyInfoExample=new CompanyInfoExample();
        CompanyInfoExample.Criteria criteria=companyInfoExample.createCriteria();
        if(gId!=null){
            criteria.andGidEqualTo(gId);
        }
        if(compName!=null){
            criteria.andCompnameLike("%"+compName+"%");
        }
        //默认仅仅查询物资管理模块,传入企业类型编码101和102
        if(compTypeId==null){
            List<Integer> list=new ArrayList<Integer>();
            list.add(101);
            list.add(102);
            criteria.andComptypeidIn(list);
        }
        else{
            criteria.andComptypeidEqualTo(compTypeId);
        }
//        if(compTypeId!=null){
//            criteria.andComptypeidEqualTo(compTypeId);
//        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<CompanyInfo> companyInfo=companyInfoMapper.selectByExample(companyInfoExample);
        PageInfo info = new PageInfo(companyInfo);
        return info;
    }

    /**
     *
     * @param companyInfo
     * @return
     */
    public int save(CompanyInfo companyInfo){
        return companyInfoMapper.insert(companyInfo);
    }

    public int delete(Integer id) {
        return companyInfoMapper.deleteByPrimaryKey(id);
    }

    public int update(CompanyInfo companyInfo){
        int code=companyInfoMapper.updateByPrimaryKey(companyInfo);
        System.out.println("更新的返回值为："+code);
        return code;
    }

    /**
     * 获取企业类型编码为所传入值的所有企业信息
     * (用于添加疫苗/饲料信息时选择下拉的供应商)
     * @param compTypeId 企业类型编码
     * @return
     */
    public List<CompanyInfo> findByCompTypeId(int compTypeId){
        CompanyInfoExample companyInfoExample=new CompanyInfoExample();
        CompanyInfoExample.Criteria criteria=companyInfoExample.createCriteria();
        criteria.andComptypeidEqualTo(compTypeId);
        List<CompanyInfo> companyInfo=companyInfoMapper.selectByExample(companyInfoExample);
        return companyInfo;
    }

    public List<CompanyInfo> findAll(){
        CompanyInfoExample companyInfoExample=new CompanyInfoExample();
        CompanyInfoExample.Criteria criteria=companyInfoExample.createCriteria();
        List<CompanyInfo> companyInfo=companyInfoMapper.selectByExample(companyInfoExample);
        return companyInfo;
    }
}
